package com.company.cloud.behavior.modules.mgr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.behavior.modules.base.entity.ScoreItemConfigInfo;
import com.company.cloud.behavior.modules.base.service.ScoreItemConfigInfoService;
import com.company.cloud.behavior.modules.cache.models.ScoreItemModel;
import com.company.cloud.behavior.modules.cache.service.BehaviorCacheService;
import com.company.cloud.behavior.modules.cache.utils.BehaviorConvertUtils;
import com.company.cloud.behavior.modules.mgr.controller.req.FavoriteMgrQuery4PageParam;
import com.company.cloud.behavior.modules.mgr.controller.req.ScoreItemQueryParam;
import com.company.cloud.behavior.modules.mgr.controller.req.ScoreItemSaveParam;
import com.company.cloud.behavior.modules.mgr.controller.req.ScoreItemUpdateParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.function.Function;

/**
 * 评分项配置
 */
@Authc
@RestController
@RequestMapping("/mgr/behavior/scoreItem")
@Api(tags="Mgr_Behavior_评分项配置")
public class ScoreItemMgrController {

	@Autowired
	private ScoreItemConfigInfoService scoreItemConfigInfoService;

	@Autowired
	private BehaviorCacheService behaviorCacheService;

	@PostMapping("/save")
	@StaffLogc(title = "Mgr_Behavior_评分项配置新增", action = "Mgr_Behavior_评分项配置新增", actionAdo = "Mgr_Behavior_评分项配置")
	@ApiOperation(value="新增评分项", response= BaseResult.class)
	public BaseResult save(@RequestBody ScoreItemSaveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		ValidatorUtils.validate(params);
		Integer count = scoreItemConfigInfoService.count(new QueryWrapper<ScoreItemConfigInfo>()
				.eq("name", params.getName())
				.eq("obj_type", params.getObjType())
				.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
		);
		if (count > 0) {
			throw new BusinessException(BaseReturnEnum.DATA_EXISTS);
		}
		ScoreItemConfigInfo entity = BehaviorConvertUtils.getScoreItemConfigInfo(params);
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		scoreItemConfigInfoService.save(entity);
		behaviorCacheService.resetScoreItemByCache(params.getObjType());
		return BaseResult.ok();
	}

	@PostMapping("/edit")
	@StaffLogc(title = "Mgr_Behavior_评分项配置修改", action = "Mgr_Behavior_评分项配置修改", actionAdo = "Mgr_Behavior_评分项配置")
	@ApiOperation(value="修改评分项", response= BaseResult.class)
	public BaseResult edit(@RequestBody ScoreItemUpdateParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		ValidatorUtils.validate(params);
		ScoreItemConfigInfo entity = scoreItemConfigInfoService.getById(params.getCode());
		if (entity == null) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity = BehaviorConvertUtils.setScoreItemConfigInfo(entity, params);
		entity.addEditParam(userInfo);
		scoreItemConfigInfoService.updateById(entity);
		behaviorCacheService.resetScoreItemByCache(params.getObjType());
		return BaseResult.ok();
	}

	@PostMapping("/query")
	@ApiOperation(value="评分项列表", response= ScoreItemModel.class)
	public BaseResult query(@RequestBody ScoreItemQueryParam params) {
		IPage<ScoreItemConfigInfo> page = scoreItemConfigInfoService.page(new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<ScoreItemConfigInfo>()
						.eq(CheckUtils.hasText(params.getObjType()),"obj_type", params.getObjType())
						.like(CheckUtils.hasText(params.getName()), "name", params.getName())
						.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
						.orderByAsc("orders")
		);
		IPage<ScoreItemModel> data = page.convert(new Function<ScoreItemConfigInfo, ScoreItemModel>() {
			@Override
			public ScoreItemModel apply(ScoreItemConfigInfo entity) {
				return BehaviorConvertUtils.getScoreItemModel(entity);
			}
		});
		return BaseResult.ok(data);
	}

	@GetMapping("/get")
	@ApiOperation(value="评分项详情", response=ScoreItemModel.class)
	public BaseResult get(@RequestParam("code")String code) {
		ScoreItemConfigInfo entity = scoreItemConfigInfoService.getById(code);
		if(null == entity) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		return BaseResult.ok(BehaviorConvertUtils.getScoreItemModel(entity));
	}

	@GetMapping("/delete")
	@StaffLogc(title = "Mgr_Behavior_评分项配置删除", action = "Mgr_Behavior_评分项配置删除", actionAdo = "Mgr_Behavior_评分项配置")
	@ApiOperation(value="删除评分项", response=ScoreItemModel.class)
	public BaseResult delete(@RequestParam("code")String code) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		ScoreItemConfigInfo entity = scoreItemConfigInfoService.getById(code);
		if(null == entity) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		entity.setRowsState(BaseConstant.EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		scoreItemConfigInfoService.updateById(entity);
		return BaseResult.ok();
	}

}
